/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.actionControll {
	import com.googlecode.milib.base.MLActionClass;
	import com.googlecode.milib.service.singleton.EnterFrameService;
	import com.googlecode.milib.vo.MLEvent;

	import flash.events.Event;

	/**
	 * @author Marek Brun
	 */
	public class ActionsEnterFrameDetonator extends MLActionClass {

		private var actions:Array;
		private var actionsToStart:Array;
		private var framesDelay:int;
		private var countFrames:Number;		public var isStart:Boolean=true;
		public var isReverse:Boolean=false;		public var isWaitToLastActionFinish:Boolean=false;
		private var efs:EnterFrameService;
		private var actionStarted:MLActionClass;

		public function ActionsEnterFrameDetonator(actions:Array, framesDelay:int) {
			this.actions=actions;
			this.framesDelay=framesDelay;
			efs=EnterFrameService.getInstance();
			
		}
		
		public function addAction(action:MLActionClass):void {
			if(!action){ throw new Error('Passed value must be non-null'); }
			actions.push(action);
		}
		
		override protected function doStart():Boolean {
			actionsToStart=actions.concat();
			if(isReverse){
				actionsToStart.reverse();
			}
			if(startNextAction()){
				efs.addEventListener(efs.event_EnterFrame, onEnterFrame);
				return true;
			}
			return false;
		}
		
		override protected function doFinish():Boolean {
			if(actionsToStart.length){ return false; }
			efs.removeEventListener(efs.event_EnterFrame, onEnterFrame);
			return true;
		}
		
		protected function startNextAction():Boolean {
			if(actionsToStart.length){
				actionStarted=MLActionClass(actionsToStart.shift());
				actionStarted.setIsRunning(isStart);
				countFrames=0;
				return true;
			}
			return false;
		}
		
		public function clear():void {
			actions=[];
		}
		
//********************************************************************************************
//		events for ActionsEnterFrameDetonator
//********************************************************************************************
		protected function onEnterFrame(event:MLEvent):void {
			countFrames++;
			if(countFrames>=framesDelay){
				if(!startNextAction()){
					if(isWaitToLastActionFinish){
						if(actionStarted.getIsRunning()==isStart){
							finish();
						}else{
							actionStarted.addEventListener(actionStarted.event_RunningChanged, onLastActionStartedRunningChanged);
						}
					}else{
						finish();
					}
				}
			}
		}
		
		protected function onLastActionStartedRunningChanged(event:Event):void {
			if(actionStarted.getIsRunning()==isStart){
				actionStarted.removeEventListener(actionStarted.event_RunningChanged, onLastActionStartedRunningChanged);
				finish();
			}
		}
	}
}
